package cn.lanqiao.web.controller;

import cn.lanqiao.common.entity.Comment;
import cn.lanqiao.common.entity.User;
import cn.lanqiao.common.utile.Result;
import cn.lanqiao.common.vo.CommentVo;
import cn.lanqiao.system.service.CommentService;
import cn.lanqiao.system.service.FilmService;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    public CommentService commentService;


    //查看所有评论
    @RequestMapping("/showAllComment")
    public Result showALlComment(Integer id){
        try {
            List<CommentVo> showAllCommentList = commentService.showAllComment(id);
            return Result.ok(showAllCommentList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.fail("系统错误");
    }


   //添加评论
    @RequestMapping("/addCommentByUser")
    public Result addCommentByUser(Comment comment){
       // Comment comment = new Comment();
        try {
            Integer rs = commentService.addComemnt(comment);
            if(rs > 0) {
                return Result.ok("评论成功~");
                }else {
                return Result.fail("评论失败~");
                }
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.fail("系统错误");
    }
   @RequestMapping("/deleteCommentByUser")
    public Result deleteCommentByUser(Comment comment){
        try {
            Integer rs = commentService.deleteComemnt(comment);
            if(rs > 0) {
                return Result.ok("删除成功~");
            }else {
                return Result.fail("删除失败~");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.fail("系统错误");
    }
    //用户写评论
    @RequestMapping("/userAddComment")
    public Result add(@RequestBody Comment comment){
        try {

             Comment com=new Comment();
             //设置评论时间
            com.setTime(new Date());
             //设置用户id
            com.setUserId(comment.getUserId());
            //设置评论id
            com.setId(comment.getId());
            //设置电影id
            com.setFilmId(comment.getFilmId());
            //设置评论内容
            com.setTextInfo(comment.getTextInfo());


            //获取评论内容
            String s=comment.getTextInfo();

            //循环遍历字符3
            for(int i=0;i<s.length();i++){
                String ss=String.valueOf(s.charAt(i));


                    //判断是否存在非法字符
                    if(ss.equals("操")||ss.equals("艹")||ss.equals("滚")){
                        return Result.ok("包含有非法字符，请重新输入！",5005);
                    }else if(i==s.length()-1){
                        int ii=commentService.userAddComment(com);
                        if(ii>0){
                            return Result.ok();
                        }
                    }
                String ff=ss+String.valueOf(s.charAt(i+1));
                     if(ff.equals("他妈")||ff.equals("你妈")||ff.equals("妈逼")||ff.equals("王八")||ff.equals("妈的")||ff.equals("贱人")){
                        return Result.ok("包含有非法字符，请重新输入！",5005);
                    }

            }


        }catch(Exception e){
            e.printStackTrace();
        }
        return Result.fail("系统出错！");
    }
}

